/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package work.konbai.education.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import work.konbai.basic.vo.MemberVO;
import work.konbai.education.dto.CheckDTO;
import work.konbai.education.entity.HomeworkDetail;
import work.konbai.education.mapper.HomeworkDetailMapper;
import work.konbai.education.service.IHomeworkDetailService;
import work.konbai.education.vo.HomeworkDetailVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作业详情信息表 服务实现类
 *
 * @author KonBAI
 * @since 2021-03-02
 */
@Service
public class HomeworkDetailServiceImpl extends ServiceImpl<HomeworkDetailMapper, HomeworkDetail> implements IHomeworkDetailService {

	@Override
	public List<MemberVO> checkDataList(Long courseId) {
		return baseMapper.selectCheckDataList(courseId);
	}

	@Override
	public List<MemberVO> submittedList(Long homeworkId) {
		return baseMapper.selectSubmittedList(homeworkId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean checkSubmit(CheckDTO checkDTO) {
		// 查询原有的学生集合
		List<HomeworkDetail> oldList = baseMapper.selectList(Condition.<HomeworkDetail>getLambdaQueryWrapper()
			.eq(HomeworkDetail::getHomeworkId, checkDTO.getHomeworkId()));
		// 筛选 原有学生ID不存在提交集合中的studentId
		List<HomeworkDetail> delStudentList = oldList.stream()
			.filter(item -> !checkDTO.getStudentIdList().contains(item.getStudentId())).collect(Collectors.toList());
		// 如果待删除列表不为空,则执行删除操作
		if (ObjectUtil.isNotEmpty(delStudentList)) {
			List<Long> delStudentIdList = delStudentList.stream().map(HomeworkDetail::getId).collect(Collectors.toList());
			if (!SqlHelper.retBool(baseMapper.deleteBatchIds(delStudentIdList))) {
				// 手动回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}
		List<Long> oldStudentIdList = oldList.stream()
			.map(HomeworkDetail::getStudentId).collect(Collectors.toList());
		// 筛选 提交学生ID不存在原有集合中的studentId
		List<Long> addStudentIdList = checkDTO.getStudentIdList().stream()
			.filter(id -> !oldStudentIdList.contains(id)).collect(Collectors.toList());
		List<HomeworkDetail> homeworkDetailList = addStudentIdList.stream().map(studentId -> {
			HomeworkDetail detail = new HomeworkDetail();
			detail.setHomeworkId(checkDTO.getHomeworkId());
			detail.setStudentId(studentId);
			return detail;
		}).collect(Collectors.toList());
		if (ObjectUtil.isNotEmpty(homeworkDetailList)) {
			if (!SqlHelper.retBool(baseMapper.insertBatchSomeColumn(homeworkDetailList))) {
				// 手动回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return false;
			}
		}
		return true;
	}

	@Override
	public IPage<HomeworkDetailVO> scoreDataPage(IPage<MemberVO> page, Long homeworkId) {
		return baseMapper.selectScoreData(page, homeworkId);
	}
}
